CREATE PROCEDURE sp_ThemKhachThuePhong
	@HoTen nvarchar(30),
	@CMND char(13),
	@DienThoai char(15),
	@DiaChi nvarchar(50),
	@MaLK int
AS
BEGIN TRAN
	if not exists(SELECT * FROM LOAIKHACH WHERE ID_LK = @MaLK)
	begin
		raiserror('Khong ton tai loai khach',16,1)
		rollback
		return
	end

	declare @Ma int
	set @Ma = 1
	while exists(SELECT * FROM KHACHTHUEPHONG WHERE MaKT = @Ma)
		set @Ma = @Ma + 1
	INSERT INTO KHACHTHUEPHONG VALUES(@Ma, @HoTen, @CMND, @DienThoai, @DiaChi, @MaLK)
	if(@@error <> 0)
		raiserror('Khong the them khach thue',16,1)
COMMIT